const moment = require('moment')
const Shipping = require('../../model/Shipping')

module.exports = {

    //获取日志列表
    changeShipping: async(req, res, next) => {
        try {
            if (req.body.id) {
                let shipping = await Shipping.findOne({ _id: req.body.id, isDelete: 0 }).exec();
                shipping.order = req.body.order,
                    shipping.tracking = req.body.tracking,
                    shipping.company = req.body.company,
                    shipping.updatedAt = req.body.updatedAt,
                    await shipping.save()
                return res.ok('修改成功', shipping)

            } else if (!req.body.id) {
                let newShipping = new Shipping({
                    order: req.body.order, // 退款订单id
                    tracking: req.body.tracking, // 快递单号,
                    company: req.body.company, // 快递公司,
                    updatedAt: new Date(),
                    isDelete: 0
                })
                await newShipping.save()
                return res.ok('创建成功', newShipping)
            }
        } catch (e) {
            next(e)
        }
    },


    deleteShipping: async(req, res, next) => {
        try {
            let shipping = await Shipping.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            shipping.isDelete = 1
            await shipping.save()
            return res.ok('删除成功', shipping)
        } catch (e) {
            next(e)
        }
    },


    getShippingList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.order) {
                option.order = new RegExp(req.body.order, 'i')
            }
            let count = await Shipping.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await Shipping.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            res.ok('获取成功', result)
        } catch (e) {
            next(e)
        }
    },

};